import jieba
import util.math as RM
import util.file as RF
import util.IO as RI
class Statistic:
    def __init__(self,path,gap):
        self.__caseList=[]
        with open(path,'r',encoding='utf-8') as f:
            data=f.read().split('\n')
            for i in range(1,data.__len__()):
                temp=data[i].split(gap)
                ID=int(temp[0])
                temp=temp[-5:]
                temp=[float(x) for x in temp]
                self.__caseList.append(Statistic.case(ID,temp))

        self.__sampleSize=data.__len__()-1

    def getCaseList(self):
        return self.__caseList
    def sortScore(self,standard,sorted):
        if standard.__len__()!=sorted.__len__():
            print("两组排序的个数必须相同")
            return
        if standard.__len__()==1:
            print("排序个数至少为2")
            return
        standardScore=0
        minScore=0
        sortedScore=0
        sampleSize=standard.__len__()
        standard.reverse()
        reversedStandard=standard.copy()
        standard.reverse()
        #计算标准排序得分,以标准排序为最高分，以reversed标准排序为最低分，计算待测排序得分
        for i in range(0,sampleSize-1):
            for j in range(1,sampleSize):
                standardScore+=j-i
                sortedScore+=j-i-abs(j-i-(sorted.index(standard[j])-sorted.index(standard[i])))
                minScore+=j-i-abs(j-i-(reversedStandard.index(standard[j])-reversedStandard.index(standard[i])))
            RI.progressBar(i,sampleSize-1)
        print()

        finalScore=(sortedScore-minScore)/(standardScore-minScore)
        return finalScore
    def sorted(self,standard,data):
        ID=[]
        for i in range(0,data.__len__()-1):
            for j in range(i+1,data.__len__()):
                if(data[i].getStudyData()[standard]>data[j].getStudyData()[standard]):
                    temp=data[i]
                    data[i]=data[j]
                    data[j]=temp
        for case in data:
            ID.append(case.getID())
        return ID
    class case:
        def __init__(self,ID,studyData):
            self.__ID=ID
            self.__studyData=studyData

        def getID(self):
            return self.__ID
        def getStudyData(self):
            return self.__studyData

if __name__ == '__main__':
    statistic=Statistic(RF.getRootPath()+'data\dataBase\storage.csv','\\')
    #reduceMatrix=RM.reduceDim(statistic.getXmatrix())
    #print(reduceMatrix)
    ID2=statistic.sorted(2,statistic.getCaseList()[10:20])
    ID1=statistic.sorted(1,statistic.getCaseList()[10:20])
    print(ID2)
    print(ID1)
    print(statistic.sortScore(ID2,ID1))
